home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / onlypc / java / samples.bin / AnimationPanel.java next >
Text File  |  1996-03-03  |  4KB  |  168 lines

  1. /*
  2.     This class is a basic extension of the Panel class.  It can be used by
  3.     either an applet or application.  To use it, add an instance to any
  4.     subclass of Container.
  5.  
  6.     example:
  7.  
  8.     add(new AnimationPanel());
  9.     //use validate if the add is done outside of
  10.     //an applets init or a Containers constructor.
  11.     validate();
  12.  
  13.     You can add controls to AnimationPanel with Cafe Studio, but not menus.  (Menus
  14.     can only be added to subclasses of Frame.)
  15.  */
  16.  
  17. import java.awt.*;
  18.  
  19. public class AnimationPanel extends Panel
  20. {
  21.     PolygonAnimation theAnimation;
  22.  
  23.     public AnimationPanel(Color color, int delay)
  24.     {
  25.         super();
  26.         setBackground(color);
  27.  
  28.         //{{INIT_CONTROLS
  29.         setLayout(new BorderLayout());
  30.         buttonPanel=new Panel();
  31.         add("South",buttonPanel);
  32.         buttonPanel.reshape(8,214,212,48);
  33.         aPanel=new Panel();
  34.         add("Center",aPanel);
  35.         aPanel.reshape(4,4,216,210);
  36.         startButton=new Button("Start");
  37.         buttonPanel.add(startButton);
  38.         startButton.reshape(34,18,60,24);
  39.         stopButton=new Button("Stop");
  40.         buttonPanel.add(stopButton);
  41.         stopButton.reshape(120,18,60,24);
  42.         //}}
  43.  
  44.         aPanel.add(theAnimation = new PolygonAnimation(delay));
  45.     }
  46.  
  47.     public void ResetAnimation()
  48.     {
  49.         theAnimation.stop();
  50.         theAnimation.position = 0;
  51.         theAnimation.repaint();
  52.     }
  53.  
  54.  
  55.     public boolean handleEvent(Event event) {
  56.         if (event.id == Event.ACTION_EVENT && event.target == stopButton) {
  57.             StopAnimation();
  58.             return true;
  59.         }
  60.         else
  61.  
  62.         if (event.id == Event.ACTION_EVENT && event.target == startButton) {
  63.             StartAnimation();
  64.             return true;
  65.         }
  66.  
  67.         return super.handleEvent(event);
  68.     }
  69.  
  70.     //{{DECLARE_CONTROLS
  71.     Panel buttonPanel;
  72.     Panel aPanel;
  73.     Button startButton;
  74.     Button stopButton;
  75.     //}}
  76.     public void StartAnimation() {
  77.         theAnimation.resume();
  78.     }
  79.  
  80.     public void StopAnimation() {
  81.         theAnimation.stop();
  82.     }
  83. }
  84.  
  85.  
  86. /*
  87.     This class is a simple extention of Canvas that runs as it's own
  88.     thread.  It runs a simple animation.
  89.  */
  90. class PolygonAnimation extends Canvas implements Runnable
  91. {
  92.     Thread  myThread;
  93.     Polygon thePolygon;
  94.     int     polyX[] = new int[9],  //The polygons X coordinates
  95.             polyY[] = new int[9],  //The polygons Y coordinates
  96.             position,               //The point on the polygon the line is drawn to
  97.             thedelay;
  98.  
  99.     public PolygonAnimation(int delay)
  100.     {
  101.         resize(130, 130);
  102.         SetupPolygon();
  103.         thePolygon = new Polygon(polyX, polyY, 9);
  104.         position = 0;
  105.  
  106.         if(delay < 0 || delay > 300) delay = 100;
  107.         else thedelay = delay;
  108.  
  109.         myThread = new Thread(this);
  110.         myThread.start();
  111.     }
  112.  
  113.     public void resume() { myThread.resume(); }
  114.  
  115.     public void run()
  116.     {
  117.         while (true)
  118.         {
  119.             repaint();
  120.             try{ Thread.sleep(thedelay); } catch(InterruptedException e) { this.stop(); }
  121.             advance();
  122.         }
  123.     }
  124.  
  125.     public void stop() { myThread.suspend(); }
  126.  
  127.     public void destroy() { this.stop(); }
  128.  
  129.     //Draw the polygon and a line from the center to the
  130.     //specified position.
  131.     public void paint(Graphics g)
  132.     {
  133.         g.drawPolygon(thePolygon);
  134.         g.drawLine(65, 65, polyX[position], polyY[position]);
  135.     }
  136.  
  137.     private void advance()
  138.     {
  139.         if(position < 6) position++;
  140.         else position = 0;
  141.     }
  142.  
  143.     //Set up the two arrays with the X and Y coordinate values
  144.     private void SetupPolygon()
  145.     {
  146.         polyX[0] = 37;
  147.         polyX[1] = 97;
  148.         polyX[2] = 129;
  149.         polyX[3] = 129;
  150.         polyX[4] = 97;
  151.         polyX[5] = 37;
  152.         polyX[6] = 1;
  153.         polyX[7] = 1;
  154.         polyX[8] = 37;
  155.  
  156.         polyY[0] = 1;
  157.         polyY[1] = 1;
  158.         polyY[2] = 37;
  159.         polyY[3] = 97;
  160.         polyY[4] = 129;
  161.         polyY[5] = 129;
  162.         polyY[6] = 97;
  163.         polyY[7] = 37;
  164.         polyY[8] = 1;
  165.     }
  166. }
  167.  
  168.